package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.bairong.BairongData;
import com.ucredit.babel.common.model.third.bairong.BairongUserReportRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 百融用户评估报告
 *
 * @author xj
 */
public class BairongUserReportParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 百融用户评估报告
     *
     * @param model
     * @throws InterruptedException
     * @throws IOException
     */
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        BairongUserReportRecord obj = (BairongUserReportRecord) model;
        BairongData scoreData = obj.getData();
        JSONObject info = JSON.parseObject(scoreData.getData());
        JSONObject flag = info.getJSONObject("Flag");
        JSONObject score = info.getJSONObject("Score");

        StringBuilder sb = getBaseInfo(obj, successful);
        //状态码 code
        sb.append(info.getString("code")).append(CRT001);
        //设备请求流水号 swift_number
        sb.append(info.getString("swift_number")).append(CRT001);
        //风险识别衍生变量输出标识 flag_scoredata
        sb.append(flag == null ? info.getString("flag_scoredata") : flag.getString("scoredata")).append(CRT001);
        //风险识别输出标识 flag_score
        sb.append(flag == null ? info.getString("flag_score") : flag.getString("score")).append(CRT001);
        //客制化评分 scorecust
        sb.append(score == null ? info.getString("scorecust") : score.getString("scorecust")).append(CRT001);

        if (info.getJSONObject("ScoreData") == null) {
            //按身份证号查询，近6个月在非银机构-持牌融资租赁机构申请机构数 als_m6_id_nbank_finlea_orgnum
            sb.append(info.getString("sd_scorecust_als_m6_id_nbank_finlea_orgnum")).append(CRT001);
            // ir_id_x_mail_cnt
            sb.append(info.getString("sd_scorecust_ir_id_x_mail_cnt")).append(CRT001);
            //按手机号查询，近15天内在非银机构-持牌网络小贷机构申请次数 alm_d15_cell_nbank_nsloan_allnum
            sb.append(info.getString("sd_scorecust_alm_d15_cell_nbank_nsloan_allnum")).append(CRT001);
            //网络购物频率 celllct_online_fre_w
            sb.append(info.getString("sd_scorecust_celllct_online_fre_w")).append(CRT001);
            //按身份证号查询，近6个月在非银机构夜间申请次数 als_m6_id_nbank_night_allnum
            sb.append(info.getString("sd_scorecust_als_m6_id_nbank_night_allnum")).append(CRT001);
            //最近360天申请的申请机构风险波动算法取值的均值 alf_var33
            sb.append(info.getString("sd_scorecust_alf_var33")).append(CRT001);
            //按手机号查询，第1个月内在非银机构-p2p申请次数 alm_m1_cell_nbank_p2p_allnum
            sb.append(info.getString("sd_scorecust_alm_m1_cell_nbank_p2p_allnum")).append(CRT001);
            //授信额度建议(积极) cme_radical
            sb.append(info.getString("sd_scorecust_cme_radical")).append(CRT001);
            //授信额度建议(审慎）cme_conservative
            sb.append(info.getString("sd_scorecust_cme_conservative")).append(CRT001);
            //近两年文化娱乐类购买总次数 cf_var34_w
            sb.append(info.getString("sd_scorecust_cf_var34_w")).append(CRT001);
            //近3个月命中最低灰名单等级 gl_ex_var8
            sb.append(info.getString("sd_scorecust_gl_ex_var8")).append(CRT001);
            //手机号使用时长 celllct_use_days_w
            sb.append(info.getString("sd_scorecust_celllct_use_days_w")).append(CRT001);
            //近3个月命中平均灰名单等级 gl_ex_var13
            sb.append(info.getString("sd_scorecust_gl_ex_var13")).append(CRT001);
            //按身份证号查询，近15天内在非银机构-其他申请机构数 alm_d15_id_nbank_oth_orgnum
            sb.append(info.getString("sd_scorecust_alm_d15_id_nbank_oth_orgnum")).append(CRT001);
            // ir_cell_x_mail_cnt
            sb.append(info.getString("sd_scorecust_ir_cell_x_mail_cnt")).append(CRT001);
            //月均消费金额 mmb_var12_w
            sb.append(info.getString("sd_scorecust_mmb_var12_w")).append(CRT001);
            //按身份证号查询，近12个月在非银机构-持牌融资租赁机构申请次数 als_m12_id_nbank_finlea_allnum
            sb.append(info.getString("sd_scorecust_als_m12_id_nbank_finlea_allnum")).append(CRT001);
            //按身份证号查询，第1个月内在非银机构-持牌网络小贷机构申请次数 alm_m1_id_nbank_nsloan_allnum
            sb.append(info.getString("sd_scorecust_alm_m1_id_nbank_nsloan_allnum")).append(CRT001);
            //按身份证号查询，近15天申请线上现金分期的次数 als_d15_id_caon_allnum
            sb.append(info.getString("sd_scorecust_als_d15_id_caon_allnum")).append(CRT001);
            //月均消费频次 mmb_var13_w
            sb.append(info.getString("sd_scorecust_mmb_var13_w")).append(CRT001);
            //过往全部申请的不同身份证号个数 alf_var50
            sb.append(info.getString("sd_scorecust_alf_var50")).append(CRT001);
            //按身份证号查询，近1个月申请线上现金分期的次数 als_m1_id_caon_allnum
            sb.append(info.getString("sd_scorecust_als_m1_id_caon_allnum")).append(CRT001);
            //按手机号查询，近15天内在非银机构-p2p申请次数 alm_d15_cell_nbank_p2p_allnum
            sb.append(info.getString("sd_scorecust_alm_d15_cell_nbank_p2p_allnum")).append(CRT001);
            //过往全部申请的申请机构风险波动算法取值的均值 alf_var25
            sb.append(info.getString("sd_scorecust_alf_var25")).append(CRT001);
            //按身份证号查询，近6个月在非银机构-持牌融资租赁机构申请次数 als_m6_id_nbank_finlea_allnum
            sb.append(info.getString("sd_scorecust_als_m6_id_nbank_finlea_allnum")).append(CRT001);
            //按身份证号查询，近15天内在非银机构-持牌网络小贷机构申请次数 alm_d15_id_nbank_nsloan_allnum
            sb.append(info.getString("sd_scorecust_alm_d15_id_nbank_nsloan_allnum")).append(CRT001);
            //关联姓名与输入姓名是否匹配 mma_var339_w
            sb.append(info.getString("sd_scorecust_mma_var339_w")).append(CRT001);
        } else {
            JSONObject scoreCust = info.getJSONObject("ScoreData").getJSONObject("scorecust");
            //按身份证号查询，近6个月在非银机构-持牌融资租赁机构申请机构数 als_m6_id_nbank_finlea_orgnum
            sb.append(info.getString("als_m6_id_nbank_finlea_orgnum")).append(CRT001);
            // ir_id_x_mail_cnt
            sb.append(info.getString("ir_id_x_mail_cnt")).append(CRT001);
            //按手机号查询，近15天内在非银机构-持牌网络小贷机构申请次数 alm_d15_cell_nbank_nsloan_allnum
            sb.append(info.getString("alm_d15_cell_nbank_nsloan_allnum")).append(CRT001);
            //网络购物频率 celllct_online_fre_w
            sb.append(info.getString("celllct_online_fre_w")).append(CRT001);
            //按身份证号查询，近6个月在非银机构夜间申请次数 als_m6_id_nbank_night_allnum
            sb.append(info.getString("als_m6_id_nbank_night_allnum")).append(CRT001);
            //最近360天申请的申请机构风险波动算法取值的均值 alf_var33
            sb.append(info.getString("alf_var33")).append(CRT001);
            //按手机号查询，第1个月内在非银机构-p2p申请次数 alm_m1_cell_nbank_p2p_allnum
            sb.append(info.getString("alm_m1_cell_nbank_p2p_allnum")).append(CRT001);
            //授信额度建议(积极) cme_radical
            sb.append(info.getString("cme_radical")).append(CRT001);
            //授信额度建议(审慎）cme_conservative
            sb.append(info.getString("cme_conservative")).append(CRT001);
            //近两年文化娱乐类购买总次数 cf_var34_w
            sb.append(info.getString("cf_var34_w")).append(CRT001);
            //近3个月命中最低灰名单等级 gl_ex_var8
            sb.append(info.getString("gl_ex_var8")).append(CRT001);
            //手机号使用时长 celllct_use_days_w
            sb.append(info.getString("celllct_use_days_w")).append(CRT001);
            //近3个月命中平均灰名单等级 gl_ex_var13
            sb.append(info.getString("gl_ex_var13")).append(CRT001);
            //按身份证号查询，近15天内在非银机构-其他申请机构数 alm_d15_id_nbank_oth_orgnum
            sb.append(info.getString("alm_d15_id_nbank_oth_orgnum")).append(CRT001);
            // ir_cell_x_mail_cnt
            sb.append(info.getString("ir_cell_x_mail_cnt")).append(CRT001);
            //月均消费金额 mmb_var12_w
            sb.append(info.getString("mmb_var12_w")).append(CRT001);
            //按身份证号查询，近12个月在非银机构-持牌融资租赁机构申请次数 als_m12_id_nbank_finlea_allnum
            sb.append(info.getString("als_m12_id_nbank_finlea_allnum")).append(CRT001);
            //按身份证号查询，第1个月内在非银机构-持牌网络小贷机构申请次数 alm_m1_id_nbank_nsloan_allnum
            sb.append(info.getString("alm_m1_id_nbank_nsloan_allnum")).append(CRT001);
            //按身份证号查询，近15天申请线上现金分期的次数 als_d15_id_caon_allnum
            sb.append(info.getString("als_d15_id_caon_allnum")).append(CRT001);
            //月均消费频次 mmb_var13_w
            sb.append(info.getString("mmb_var13_w")).append(CRT001);
            //过往全部申请的不同身份证号个数 alf_var50
            sb.append(info.getString("alf_var50")).append(CRT001);
            //按身份证号查询，近1个月申请线上现金分期的次数 als_m1_id_caon_allnum
            sb.append(info.getString("als_m1_id_caon_allnum")).append(CRT001);
            //按手机号查询，近15天内在非银机构-p2p申请次数 alm_d15_cell_nbank_p2p_allnum
            sb.append(info.getString("alm_d15_cell_nbank_p2p_allnum")).append(CRT001);
            //过往全部申请的申请机构风险波动算法取值的均值 alf_var25
            sb.append(info.getString("alf_var25")).append(CRT001);
            //按身份证号查询，近6个月在非银机构-持牌融资租赁机构申请次数 als_m6_id_nbank_finlea_allnum
            sb.append(info.getString("als_m6_id_nbank_finlea_allnum")).append(CRT001);
            //按身份证号查询，近15天内在非银机构-持牌网络小贷机构申请次数 alm_d15_id_nbank_nsloan_allnum
            sb.append(info.getString("alm_d15_id_nbank_nsloan_allnum")).append(CRT001);
            //关联姓名与输入姓名是否匹配 mma_var339_w
            sb.append(info.getString("mma_var339_w")).append(CRT001);
        }
        StringBuilder txSb = getBaseInfo(obj);
        key.set(getKey());
        val.set((sb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_bairong_userreport_inc";
    }

    @Override
    public String getType() {
        return RecordType.BAIRONG_USERREPORT_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "百融用户评估报告";
    }

}
